package com.xmz.controller;

import com.xmz.util.Result;
import com.xmz.entity.Student;
import com.xmz.service.StudentService;
import com.xmz.util.PageResult;
import com.xmz.vo.StudentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;


@RestController
@RequestMapping("/student")
@CrossOrigin
public class StudentController {

    @Autowired
    private StudentService studentService;

    @GetMapping("/findAll")
    public List<Student>  selectAllStudents(){
        List<Student> students = studentService.selectAll();
        System.out.println(students);
        return students;
    }

    @GetMapping("/selectStudentPage")
    public PageResult<Student> findByPage( Integer page,Integer pageSize){
        return studentService.findByPage(page,pageSize);
    }
    /*
        查询对象非必须
     */
    @PostMapping("/searchStudent")
    public PageResult<Student> search(Integer page,
                                      Integer pageSize,
                                      @RequestBody(required = false) StudentVo searchStudent){

        System.out.println(page+"_"+pageSize);
        System.out.println(searchStudent);

        return studentService.search(page,pageSize,searchStudent);
    }

    @PostMapping("/addStudent")
    public Result addStudent(@RequestBody Student student){
        int i = studentService.addStudent(student);
        if(i > 0) return Result.success("添加成功！");
        return Result.error("添加失败！");
    }

}
